/*
 * @(#)JsonMarshaller.java
 * Date 2013-11-03
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.ihecommon.service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.enterprise.context.ApplicationScoped;

import org.codehaus.jackson.map.ObjectMapper;

import us.hornerscorners.lamppost.transformer.IJsonMarshaller;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-11-03
 * @author         Jim Horner
 */
@ApplicationScoped
public class JsonMarshaller implements IJsonMarshaller {

    /** Field description */
    private final ObjectMapper objectMapper;

    /**
     * Constructs ...
     *
     */
    public JsonMarshaller() {

        super();

        this.objectMapper = new ObjectMapper();
    }

    /**
     * Method description
     *
     *
     * @param item
     *
     * @return
     */
    @Override
    public String marshal(Object item) throws IOException {

        return this.objectMapper.writeValueAsString(item);
    }

    /**
     * Method description
     *
     *
     * @param source
     * @param peristableClass
     * @param <T>
     *
     * @return
     *
     * @throws IOException
     */
    @Override
    public <T> T unmarshal(byte[] source, Class<T> peristableClass) throws IOException {

        return this.objectMapper.readValue(source, peristableClass);
    }

    /**
     * Method description
     *
     *
     * @param bas
     * @param aThis
     *
     * @throws IOException
     */
    @Override
    public void writeValue(ByteArrayOutputStream bas, Object aThis) throws IOException {

        this.objectMapper.writeValue(bas, aThis);
    }
}
